﻿//--------------------------------------------------------------------------
// <copyright file="Mails.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <summary>Mails</summary>
//--------------------------------------------------------------------------
namespace MS.Msn.MediaSolutions.Common
{
    using System;
    using System.Net.Sockets;
    using System.IO;
    using System.Text;
    using System.Data;
    using System.Net;
    using System.Collections;
    using System.Collections.ObjectModel;
    using System.Collections.Generic;
    using System.Net.Mail;

    /// <summary>
    /// SendMailHelper
    /// </summary>
    public class Mails
    {
        #region Properties

        private string mailFromDisplayName = "Msn Media International";

        /// <summary>
        /// From Display Name
        /// </summary>
        public string MailFromDisplayName
        {
            set { this.mailFromDisplayName = value; }
            get { return this.mailFromDisplayName; }
        }

        private string mailConent = string.Empty;

        /// <summary>
        /// MailConent
        /// </summary>
        public string MailConent
        {
            get { return mailConent; }
            set { mailConent = value; }
        }

        private string mailTitle = string.Empty;

        /// <summary>
        /// MailTitle
        /// </summary>
        public string MailTitle
        {
            get { return mailTitle; }
            set { mailTitle = value; }
        }

        private readonly IList<string> mailToAdds = new Collection<string>();

        /// <summary>
        /// MailToAdds
        /// </summary>
        public IList<string> MailToAdds
        {
            get { return mailToAdds; }
        }

        private string errorString_NoMailTo = "Not set target mail address";

        /// <summary>
        /// ErrorString_NoMailTo
        /// </summary>
        public string ErrorString_NoMailTo
        {
            get { return errorString_NoMailTo; }
            set { errorString_NoMailTo = value; }
        }

        private string errorString_LogInError = "Invalid username/password";

        /// <summary>
        /// ErrorString_LogInError
        /// </summary>
        public string ErrorString_LogInError
        {
            get { return errorString_LogInError; }
            set { errorString_LogInError = value; }
        }

        private string errorString_SmtpFailedRecipient = "Smtp failed recipient";

        /// <summary>
        /// ErrorString_SmtpFailedRecipient
        /// </summary>
        public string ErrorString_SmtpFailedRecipient
        {
            get { return errorString_SmtpFailedRecipient; }
            set { errorString_SmtpFailedRecipient = value; }
        }

        private string errorString_SmtpException = "Smtp server error";

        /// <summary>
        /// ErrorString_SmtpException
        /// </summary>
        public string ErrorString_SmtpException
        {
            get { return errorString_SmtpException; }
            set { errorString_SmtpException = value; }
        }

        #region attachments

        private readonly IList<string> attachedFiles = new Collection<string>();

        /// <summary>
        /// Attached Files
        /// </summary>
        public IList<string> AttachedFiles
        {
            get { return attachedFiles; }
        }

        private readonly IDictionary<string, Stream> attachedStreams = new Dictionary<string, Stream>();

        /// <summary>
        /// Attached Streams
        /// </summary>
        public IDictionary<string, Stream> AttachedStreams
        {
            get { return attachedStreams; }
        }

        #endregion

        #endregion

        #region Public Method
        /// <summary>
        /// Send Mail
        /// </summary>
        /// <returns>error messages</returns>
        public string Send()
        {
            try
            {
                if (MailToAdds.Count == 0)
                {
                    return this.ErrorString_NoMailTo;
                }

                SmtpClient smtp = new SmtpClient();
                MailMessage mail = new MailMessage();

                foreach (string mailto in MailToAdds)
                {
                    if (!string.IsNullOrEmpty(mailto))
                    {
                        MailAddress mdress = new MailAddress(mailto.ToLower(Apps.DefaultCulture));
                        if (!mail.To.Contains(mdress))
                        {
                            mail.To.Add(mdress);
                        }
                    }
                }

                if (mail.To.Count == 0)
                {
                    return this.ErrorString_NoMailTo;
                }

                mail.From = new MailAddress(mail.From.Address, this.MailFromDisplayName);
                mail.Body = MailConent;
                mail.Subject = MailTitle;
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.IsBodyHtml = true;

                #region add attachments

                if (!object.Equals(AttachedFiles, null) && (AttachedFiles.Count > 0))
                {
                    foreach (string fl in AttachedFiles)
                    {
                        Attachment atch = new Attachment(fl);
                        mail.Attachments.Add(atch);
                    }
                }

                if (!object.Equals(AttachedStreams, null) && (AttachedStreams.Keys.Count > 0))
                {
                    foreach (string key in AttachedStreams.Keys)
                    {
                        Stream stm = AttachedStreams[key];
                        Attachment atch = new Attachment(stm, key);
                        mail.Attachments.Add(atch);
                    }
                }

                #endregion

                //#if (DEBUG)
                //                mail.From = new MailAddress("canondbuser@163.com", this.MailFromDisplayName);
                //                smtp.Host = "smtp.163.com";
                //                smtp.Port = 25;
                //                smtp.Credentials = new System.Net.NetworkCredential("canondbuser", "MicrositeTest");
                //#endif
                smtp.Send(mail);

                if (!object.Equals(AttachedStreams, null) && AttachedStreams.Count > 0)
                {
                    foreach (Stream stm in AttachedStreams.Values)
                    {
                        if (stm.CanRead)
                        {
                            stm.Close();
                            stm.Dispose();
                        }
                    }
                }

                return string.Empty;
            }
            catch (System.Web.HttpException)
            {
                return this.ErrorString_LogInError;
            }
            catch (SmtpFailedRecipientException)
            {
                return this.ErrorString_SmtpFailedRecipient;
            }
            catch (SmtpException)
            {
                return this.ErrorString_SmtpException;
            }
        }
        #endregion
    }
}
